package com.fenghuajueli.module_user.ui.helpcenter

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.fenghuajueli.libbasecoreui.data.entity.goods.GoodsEntity
import com.fenghuajueli.module_user.model.UserViewModel
import com.fenghuajueli.module_user.network.model.helpcenter.HelpCenterData
import com.fenghuajueli.module_user.network.model.helpcenter.NetworkHelpCenter
import com.fenghuajueli.module_user.network.retrofit.UserNetwork
import com.kwad.sdk.core.b.a.it
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

/**
 * @Author: sz_ijj
 * @Date: 2024-03-30 15:28
 **/
fun getType(title: String) = when (title) {
    "使用教程" -> 4
    "会员问题" -> 2
    "广告问题" -> 3
    else -> 1
}

fun getTitle(type: Int) = when (type) {
    1 -> "功能异常"
    2 -> "会员问题"
    3 -> "广告问题"
    else -> "使用教程"
}

class HelpCenterViewModel(
    private val type: Int
) : ViewModel() {
    private val _helpCenterData = MutableStateFlow(NetworkHelpCenter())
    val helpCenterData: StateFlow<NetworkHelpCenter> = _helpCenterData

    val userViewModel = UserViewModel()
    val subscriptionState: MutableLiveData<MutableList<GoodsEntity>> = userViewModel.goodsList

    init {
        initialHelpCenterData()
    }

    private fun initialHelpCenterData() = viewModelScope.launch(Dispatchers.Default) {
        _helpCenterData.update {
            it.copy(data_list = when (type) {
                1 -> functionalAbnormalities
                2 -> membershipIssues
                3 -> advertisingIssues
                else -> emptyList()
            })
        }

        runCatching {
            val result = UserNetwork.getHelpCenter(type)
            when (result.code) {
                200 -> result.data.data_list
                else -> emptyList()
            }
        }.onFailure {
            LogUtils.e(it.message)
        }.onSuccess { newData ->
            LogUtils.d("帮助中心数据:${newData}")
            _helpCenterData.update { oldValue->
                oldValue.copy(data_list = oldValue.data_list.plus(newData.filterNot { it.status == 2 }))
            }
        }
    }

    fun initGoods(lifecycleOwner: LifecycleOwner){
        userViewModel.getGoodsList(lifecycleOwner)
    }

    private val functionalAbnormalities = listOf(
        HelpCenterData(
            question = "使用功能过程中出现闪退？",
            answer = "首先，请确认您目前安装的APP版本为应用商店可下载的最新版本。\n" +
                    "若在最新版本中仍出现功能闪退问题，请您点击下方联系客服，向客服描述您的问题，并提供您的手机型号、问题录屏，我们将尽快为您解决问题。"
        ),
        HelpCenterData(
            question = "使用功能过程中出现报错？",
            answer = "首先，请确认您目前安装的APP版本为应用商店可下载的最新版本。\n" +
                    "若在最新版本中仍出现功能报错问题，请您点击下方联系客服，向客服描述您的问题，并提供您的手机型号、问题录屏，我们将尽快为您解决问题。"
        ),
        HelpCenterData(
            question = "没有我想要的功能？",
            answer = "如果目前的产品版本中没有您想要的功能，欢迎点击下方意见反馈或联系客服，描述您的问题需求。\n" +
                    "感谢您对产品提出的意见建议，我们将继续完善产品体验。"
        ),
    )

    private val membershipIssues = listOf(
        HelpCenterData(
            question = "开通会员后，仍显示为普通用户身份？",
            answer = "如果您完成会员开通支付后，仍显示为普通用户身份，无法解锁付费功能，请您点击下方联系客服，向客服描述您的问题，并提供您的支付订单截图，我们将尽快为您恢复会员。"
        ),
        HelpCenterData(
            question = "如何取消订阅？",
            answer = "在“支付宝”-“我的”-“右上角设置”-“支付设置”-“免密支付/自动扣款”-找到对应服务“关闭服务”即可取消订阅。"
        ),
        HelpCenterData(question = "如何申请退款？", answer = ""),
    )

    private val advertisingIssues = listOf(
        HelpCenterData(
            question = "广告无法关闭？",
            answer = "大部分广告可在广告的右上角找到“跳过”按钮或者“×”按钮进行关闭。"
        ),
        HelpCenterData(
            question = "广告太多？",
            answer = "如果您感到APP内广告太多，您可以考虑开通会员，开通会员后产品仅会保留启动时的开屏广告，广告打扰将大大减少。"
        ),
        HelpCenterData(question = "在广告页面内被扣费？", answer = ""),
    )
}